﻿#include "LightToolBar.h"

const int LeftLightChannel = 0;
const int RightLightChannel = 1;
LightToolBar::LightToolBar(QWidget *parent)
{
    value[0] = value[1] = value[2] = value[3] = 100;
    m_pActionLight[0] = new QAction(tr("Turn on/off light 1"), this);
    m_pActionLight[0]->setIcon(QIcon(":/res/light-off-1.png"));
    m_pActionLight[0]->setCheckable(true);
    addAction(m_pActionLight[0]);
    connect(m_pActionLight[0], &QAction::triggered, this, &LightToolBar::onAction);

    m_pActionLight[1] = new QAction(tr("Turn on/off light 2"), this);
    m_pActionLight[1]->setIcon(QIcon(":/res/light-off-2.png"));
    m_pActionLight[1]->setCheckable(true);
    addAction(m_pActionLight[1]);
    connect(m_pActionLight[1], &QAction::triggered, this, &LightToolBar::onAction);
}

LightToolBar::~LightToolBar()
{
    lights.setValue(LeftLightChannel, 0);
    lights.setValue(RightLightChannel, 0);
}


void LightToolBar::loadSettings(QSettings &settings)
{
    value[LeftLightChannel] = settings.value("LightIntensity/LeftLight", 100).toInt();
    value[RightLightChannel] = settings.value("LightIntensity/RightLight", 100).toInt();
}

void LightToolBar::onAction(bool checked)
{
    QAction *pAction = (QAction *)sender();
    if(pAction == m_pActionLight[0])
    {
        if(checked)
        {
            m_pActionLight[0]->setIcon(QIcon(":/res/light-on-1.png"));
            lights.setValue(LeftLightChannel, value[LeftLightChannel]);
            return;
        }
        m_pActionLight[0]->setIcon(QIcon(":/res/light-off-1.png"));
        lights.setValue(LeftLightChannel, 0);
        return;
    }

    if(pAction == m_pActionLight[1])
    {
        if(checked)
        {
            m_pActionLight[1]->setIcon(QIcon(":/res/light-on-2.png"));
            lights.setValue(RightLightChannel, value[RightLightChannel]);
            return;
        }
        m_pActionLight[1]->setIcon(QIcon(":/res/light-off-2.png"));
        lights.setValue(RightLightChannel, 0);
        return;
    }
}
